package com.car.rental.order.listener;

import com.car.rental.order.service.PaymentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import com.car.rental.order.config.RabbitMQConfig;

import java.io.IOException;
import java.util.Map;

@Slf4j
@Component
@RequiredArgsConstructor
public class PaymentMessageListener {

    private final PaymentService paymentService;

    /**
     * 监听支付状态变更消息
     */
    @RabbitListener(queues = RabbitMQConfig.PAYMENT_STATUS_QUEUE)
    public void handlePaymentStatusChange(Map<String, Object> message, Channel channel, Message originalMessage) {
        try {
            String paymentNo = (String) message.get("paymentNo");
            Long vehicleId = (Long) message.get("vehicleId");
            
            log.info("收到支付状态变更消息，支付流水号：{}，车辆ID：{}", paymentNo, vehicleId);
            
            // 处理支付状态变更逻辑
            
            channel.basicAck(originalMessage.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            try {
                channel.basicNack(originalMessage.getMessageProperties().getDeliveryTag(), false, true);
            } catch (IOException ex) {
                log.error("消息确认失败", ex);
            }
            log.error("处理支付状态变更消息失败", e);
        }
    }
} 